package stack1;

import java.util.Stack;

public class Test {


    public static void main(String[] args) {
        String[] tokens={"4","13","5","/","+"};
        System.out.println(evalRPN(tokens));
    }
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<tokens.length;i++){
            String s=tokens[i];
            if(isOperator(s)){
                int r=stack.pop();
                int l=stack.pop();
                switch(s){
                    case "+":
                        stack.push(l+r);
                        break;
                    case "-":
                        stack.push(l-r);
                        break;
                    case "*":
                        stack.push(l*r);
                        break;
                    case "/":
                        stack.push(l/r);
                        break;
                }
            }else{
                int a=Integer.parseInt(s);
                stack.push(a);
            }
        }
        return stack.pop();
    }
    private static boolean isOperator(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }
        return false;
    }


    public static void main2(String[] args) {
        boolean f=isValid("[]{}()");
        System.out.println(f);
    }

    public static void main1(String[] args) {
        MyStack stack=new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(4);
        stack.push(8);

        stack.display();
        System.out.println(stack.peek());
        stack.display();
        System.out.println(stack.pop());
        stack.display();


    }

    public static boolean isValid(String s) {
        if(s==null||s.length()==1)return false;

        Stack<Character> stack=new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);

            if(ch=='('||ch=='['||ch=='{'){
                stack.push(ch);
            }else if(ch==')'||ch==']'||ch=='}'){
                char ch1=stack.peek();
                if(ch1=='('&&ch==')'||ch1=='['&&ch==']'||ch1=='{'&&ch=='}'){
                    stack.pop();
                }
            }
        }
        if(stack.empty()){
            return true;
        }else{
            return  false;
        }

    }

    //public void print

}
